<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_SESSION['onemerchurl']){
	exi('','',$_SESSION['onemerchurl']);
}
if($_RQ['op']=='main'){
	$return['list']=array();
	$return['pcd']=$this->session('pcd')?:array('city'=>'');
	$return['params']=array('order'=>'','pcd'=>$this->session('pcd')?:array('city'=>''));
	set_html('title','小区列表');
	exi($return);
}
if($_RQ['op']=='getlist'){
	if($_RQ['lat'] || $_RQ['lng']){
		$lat_lng=array('lat'=>$_RQ['lat'],'lng'=>$_RQ['lng']);
		$this->session('lat_lng',$lat_lng);
	}
	if($_RQ['city'] || $_RQ['nocity']){
		$pcd=array('province'=>$_RQ['province'],'city'=>$_RQ['city'],'district'=>$_RQ['district'],'street'=>$_RQ['street']);
		$this->session('pcd',$pcd);
	}
	$lat_lng=$this->session('lat_lng')?:array('lat'=>'31.235929','lng'=>'121.480539');
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1);
	if(trim($_RQ['keyword'])){
		$params['title like']='%'.trim($_RQ['keyword']).'%';
	}
	$pindex = max(1, 1);
	$psize = 500;
	$total='';
	$list=pdo_getslice('jy_merch_sns',$params,array($pindex,$psize),$total,$fields,'','id desc');
	foreach($list as $k=>&$v){
		$v['image']=tomedia($v['image']);
		$v['pcd']=json_decode($v['pcd'],true);
		if(trim($_RQ['province']) && $v['pcd']['province']!=trim($_RQ['province'])){
			unset($list[$k]);
			continue;
		}
		if(trim($_RQ['city']) && $v['pcd']['city']!=trim($_RQ['city'])){
			unset($list[$k]);
			continue;
		}
		if(trim($_RQ['district']) && $v['pcd']['district']!=trim($_RQ['district'])){
			unset($list[$k]);
			continue;
		}
		$v['lat_lng']=json_decode($v['lat_lng'],true);
		$v['juli']=cfc('tools')->getdistance($lat_lng['lng'],$lat_lng['lat'],$v['lat_lng']['lng'],$v['lat_lng']['lat']);
		$v['juli']=intval($v['juli']/1000);
	}
	paixu($list,'juli');
	if($newstore){
		array_unshift($list,$newstore);
	}
	$_RQ['limit']=$_RQ['limit']?:$_RQ['num'];
	if($_RQ['limit']){
		$list=array_slice($list,0,intval($_RQ['limit']));
	}else{
		$page=intval($_RQ['page'])?:1;
		$start=($page-1)*10;
		$list=array_slice($list,$start,10);
	}
	$return['list']=$list;
	if($_RQ['islocation'] && !$_SESSION['store_id']){
		$_SESSION['store_id']=$list[0]['id'];
	}
	exi($return);
}
if($_RQ['op']=='get_juli'){
	if($_RQ['lat'] || $_RQ['lng']){
		$lat_lng=array('lat'=>$_RQ['lat'],'lng'=>$_RQ['lng']);
	}
	$store_id=intval($_RQ['store_id'])?:$_SESSION['store_id'];
	$storelat_lng=pdo_getcolumn('jy_weishop_store',array('id'=>$store_id),'lat_lng');
	$storelat_lng=json_decode($storelat_lng,true);
	$juli=cfc('tools')->getdistance($lat_lng['lng'],$lat_lng['lat'],$storelat_lng['lng'],$storelat_lng['lat']);
	$juli=intval($juli/1000);
	$juli=$juli?:0;
	exi($juli);
}
if($_RQ['op']=='getcitys'){
	$citys=cfc('area')->get_citys($_RQ['py'],$_RQ['name']);
	$return['citys']=$citys;
	exi($return);
}
if($_RQ['op']=='getpcd'){
	if(!empty($_RQ['keyword'])){
			$params['title like']='%'.$_RQ['keyword'].'%';
			$return['params']['title']=$_RQ['keyword'];
	}
	$areas=cfc('area')->get_pcd_titlelist($_RQ['keyword']);
	$return['areas']=$areas;
	exi($return);
}
if($_RQ['op']=='uppcd'){
	$post=get_postdata();
	if($post['lat'] || $post['lng']){
		$lat_lng=array('lat'=>$post['lat'],'lng'=>$post['lng']);
		$this->session('lat_lng',$lat_lng);
	}
	$this->session('pcd',$post);
	//print_r($this->session('pcd'));exit;
	exi('更新成功！');
}
?>